import { Message } from 'element-ui'
export const state = () => ({
    cartList: [],
})

export const actions = {
    /** 添加购物车 */
    async addCart({ commit, dispatch }, payload) {
        const token = this.state.userStore.userInfo?.token
        if (token) {
            const { data: res } = await this.$addCart({
                skuId: payload.skuId,
                count: payload.count,
            })
            if (res.code !== '1') return Message.error(res.msg)
            dispatch('getCartList')
            Message.success('添加成功')
        } else {
            commit('setCartList', payload)
        }
    },
    /** 获取购物车列表 */
    async getCartList({ commit }) {
        const { data: cart } = await this.$getCartList()
        if (cart.code !== '1') return Message.error(cart.msg)
        commit('coverCart', cart.result)
    },
    /** 合并购物车 */
    async mergeCart({ state, dispatch }) {
        const localCart = await state.cartList.map((item) => {
            return {
                skuId: item.skuId,
                count: item.count,
                selected: item.selected,
            }
        })
        const { data: res } = await this.$mergeCart(localCart)
        if (res.code !== '1') return Message.error(res.msg)
        dispatch('getCartList')
    },
    /** 删除购物车 */
    async delCart({ commit, dispatch }, payload) {
        const token = this.state.userStore.userInfo?.token
        if (token) {
            const { data: res } = await this.$delCart(Array(payload.skuId))
            if (res.code !== '1') return Message.error(res.msg)
            dispatch('getCartList')
            Message.info('删除成功')
        } else {
            commit('delCart', payload)
        }
    },
    /** 更新购物车 */
    async updateCart({ commit, dispatch }, payload) {
        const token = this.state.userStore.userInfo?.token
        if (token) {
            const { data: res } = await this.$updateCart(payload.skuId, { selected: payload.selected, count: payload.count })
            if (res.code !== '1') return Message.error(res.msg)
            dispatch('getCartList')
        } else {
            commit('updateCart', payload)
        }

    },
    /** 改变商品选中状态 */
    async subCheckChange({ commit, dispatch }, payload) {
        const token = this.state.userStore.userInfo?.token
        if (token) {
            const { data: res } = await this.$updateCart(payload.skuId, { selected: !payload.selected, count: payload.count })
            if (res.code !== '1') return Message.error(res.msg)
            dispatch('getCartList')
        } else {
            commit('subCheckChange', payload)
        }
    },
    /** 全选/反选 */
    async changeAllChecked({ commit, dispatch, state }, payload) {
        const token = this.state.userStore.userInfo?.token
        if (token) {
            const { data: res } = await this.$updateCartAll({ selected: payload, ids: [...state.cartList.map(item => item.skuId)] })
            if (res.code !== '1') return Message.error(res.msg)
            dispatch('getCartList')
        }
        commit('changeAllChecked', payload)
    }

}

export const mutations = {
    /** 添加购物车 */
    setCartList(state, payload) {
        const result = state.cartList.find(item => item.id === payload.id)
        if (result) {
            result.count += payload.count
        } else {
            state.cartList.push(payload)
        }
        this.$cookies.set('cartList', state.cartList)
        Message.success('添加成功')
    },
    /** 查看购物车 */
    getCartList(state) {
        state.cartList = this.$cookies.get('cartList') || []
    },
    /** 删除购物车商品 */
    delCart(state, payload) {
        state.cartList = state.cartList.filter(item => item.skuId !== payload.skuId)
        this.$cookies.set('cartList', state.cartList)
        Message.info('删除成功')
    },
    /** 更新购物车商品 */
    updateCart(state, payload) {
        state.cartList = state.cartList.map(item => {
            if (item.skuId === payload.skuId) {
                item.count = payload.count
            }
            return item
        })
        this.$cookies.set('cartList', state.cartList)
    },
    /** 覆盖购物车列表 */
    coverCart(state, payload) {
        state.cartList = payload
        this.$cookies.set('cartList', state.cartList)
    },
    /** 改变商品选中状态 */
    subCheckChange(state, payload) {
        state.cartList = state.cartList.map(item => {
            if (item.skuId === payload.skuId) {
                item.selected = !payload.selected
            }
            return item
        })
        this.$cookies.set('cartList', state.cartList)
    },
    /** 全选/反选 */
    changeAllChecked(state, payload) {
        state.cartList.forEach((item) => {
            item.selected = payload
        })
        this.$cookies.set('cartList', state.cartList)
    },
    /** 清空本地购物车 */
    clearCart(state) {
        this.$cookies.remove('cartList')
        state.cartList = []
    }

}

export const getters = {
    // 商品种类数
    cartLength(state) {
        return state.cartList.length
    },
    // 商品总数
    total(state) {
        return state.cartList.reduce((pre, item) => pre + item.count, 0)
    },
    // 商品总价
    priceTotal(state) {
        return state.cartList.reduce((pre, item) => pre + item.price * item.count, 0)
    },
    // 是否全选
    checkAll(state) {
        return state.cartList.every(item => item.selected)
    },
    // 选中商品数
    checkNum(state) {
        return state.cartList.filter(item => item.selected).length
    },
    // 选中商品总价
    checkPriceTotal(state) {
        return state.cartList.filter(item => item.selected).reduce((pre, item) => pre + item.price * item.count, 0)
    }
}